﻿using BootstrapBlazor.Components;
using Mapster;
using Microsoft.AspNetCore.Components;
using OnWork.DataCenter.BLL.DataCenter;
using OnWork.DataCenter.DAL.DataCenter;
using OnWork.DataCenter.Shared.Common;
using OnWork.DataCenter.Shared.Pages.DataStandard.Designer;
using System.Security.Cryptography.X509Certificates;

namespace OnWork.DataCenter.Shared.Pages.DataSource
{
    public partial class MqttStorageView : ComponentBaseDataCenter
    {
        /// <summary>
        /// 依赖注入 提示信息
        /// </summary>
        [Inject]
        protected DSMqttStorageBLL dBConnBLL { get; set; }

        /// <summary>
        /// 数据库类型列表
        /// </summary>
        private IEnumerable<SelectedItem> TopicItems { get; set; }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            await base.OnAfterRenderAsync(firstRender);
            if (firstRender)
            {
                var topics = await dBConnBLL.QueryALLMqttTopicAsync();
                TopicItems = topics.Select(x => new SelectedItem() { Text = x.Name, Value = x.ID.ToString() }).ToList();
            }
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private async Task<QueryData<MqttStoragVO>> QueryAsync(QueryPageOptions options)
        {
            var result = await dBConnBLL.QueryPagedListAsync(options.ToBaseQueryPageInput());
            //对象转换
            return result.ToQueryData<MqttStoragVO, DSMqttStorage>();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        private async Task<bool> OnDeleteAsync(IEnumerable<MqttStoragVO> item)
        {
            var result = await dBConnBLL.DeleteDataAsync(item.Select(x => x.ID).ToArray());

            return await ShowSqlReturnMsg(result);
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="item"></param>
        /// <param name="changedType"></param>
        /// <returns></returns>
        private async Task<bool> OnSaveAsync(MqttStoragVO item, ItemChangedType changedType)
        {
            //RESTfulResult<object> result;
            if (changedType == ItemChangedType.Update) // 更新
            {
                var result = await dBConnBLL.UpdateDataAsync(item.Adapt<DSMqttStorage>());
                return await ShowSqlReturnMsg(result);
            }
            else //新增
            {
                var result = await dBConnBLL.AddDataAsync(item.Adapt<DSMqttStorage>());
                return await ShowSqlReturnMsg(result);
            }
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <returns></returns>
        private async Task OnConfigClickAsync(MqttStoragVO Config)
        {
            var result = await DialogService.ShowModal<StandardDesignerDialog>(new ResultDialogOption()
            {
                Title = "流程编辑",
                ButtonNoText = "取消",
                ButtonYesText = "保存",
                ComponentParameters = new Dictionary<string, object>
                {
                    [nameof(StandardDesignerDialog.ValueChanged)] = EventCallback.Factory.Create<(string, string)>(this, v =>
                    {
                        Config.DiagramData = v.Item1; //获取图JSOn值
                        Config.TaskConfigView = v.Item2; //获取配置的JSOn值
                    }),
                    [nameof(StandardDesignerDialog.OnLoadedAction)] = new Action<StandardDesigner>(x =>
                    {
                        if (!string.IsNullOrEmpty(Config.DiagramData))
                        {
                            x.LoadFormjson(Config.DiagramData); //初始化完成后加载配置
                        }
                    }),
                }
            });
        }
    }
}